package me.zhengjie.modules.workorder.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.workorder.domain.TAccident;
import me.zhengjie.modules.workorder.service.TAccidentService;
import me.zhengjie.modules.workorder.service.dto.TAccidentQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author zcw
* @date 2019-08-10
*/
@RestController
@RequestMapping("api")
public class TAccidentController {

    @Autowired
    private TAccidentService tAccidentService;

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Log("查询TAccident")
    @GetMapping(value = "/tAccident")
    public ResponseEntity getTAccidents(TAccidentQueryCriteria criteria,
                                        @PageableDefault(sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable){
        return new ResponseEntity(tAccidentService.queryAll(criteria,pageable),HttpStatus.OK);
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @GetMapping(value = "/tAccident/order")
    public ResponseEntity getTAccidentsWithOrder(TAccidentQueryCriteria criteria){
        return new ResponseEntity(tAccidentService.queryAllWithOrder(criteria),HttpStatus.OK);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Log("新增TAccident")
    @PostMapping(value = "/tAccident")
    public ResponseEntity create(@Validated @RequestBody TAccident resources){
        try {
            return new ResponseEntity(tAccidentService.create(resources), HttpStatus.CREATED);
        }catch (Exception ex) {
            ex.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @SuppressWarnings("rawtypes")
	@Log("修改TAccident")
    @PutMapping(value = "/tAccident")
    public ResponseEntity update(@Validated @RequestBody TAccident resources){
        try {
            tAccidentService.update(resources);
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }catch(Exception ex) {
            ex.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @SuppressWarnings("rawtypes")
	@Log("删除TAccident")
    @DeleteMapping(value = "/tAccident/{id}")
    public ResponseEntity delete(@PathVariable Long id){
        tAccidentService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}